package config

import (
	"encoding/json"
	"fmt"
	"os"
	"shop/utils/httputil"

	"gopkg.in/yaml.v2"
)

type config struct {
	// Mysql配置
	MysqlAddress  string `yaml:"mysqlAddress"`
	MysqlDBName   string `yaml:"mysqlDBName"`
	MysqlUsername string `yaml:"mysqlUsername"`
	MysqlPassword string `yaml:"mysqlPassword"`

	// Redis配置
	RedisAddress  string `yaml:"redisAddress"`
	RedisPassword string `yaml:"redisPassword"`

	// 腾讯云；api秘钥
	TencentCloudAppId     string `yaml:"tencentCloudAppId"`
	TencentCloudSecretId  string `yaml:"tencentCloudSecretId"` // 请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
	TencentCloudSecretKey string `yaml:"tencentCloudSecretKey"`
	// 腾讯云存储cos配置
	TencentCloudCosDomain string `yaml:"tencentCloudCosDomain"` // 腾讯云cos桶域名
	// 腾讯云短信sms配置, see: https://console.cloud.tencent.com/smsv2
	TencentCloudSmsAppId      string `yaml:"tencentCloudSmsAppId"`
	TencentCloudSmsAppKey     string `yaml:"tencentCloudSmsAppKey"`
	TencentCloudSmsTemplateId string `yaml:"tencentCloudSmsTemplateId"` // 玩家注册验证码短信模板, eg:模板-1087068的内容: 您的注册验证码：{1}，{2}秒内有效，请勿泄露。
	TencentCloudSmsSignName   string `yaml:"tencentCloudSmsSignName"`   // 申请的签名

	// 腾讯Auth2.0应用
	TencentAuth20AppId  string `yaml:"tencentAuth20AppId"`
	TencentAuth20AppKey string `yaml:"tencentAuth20AppKey"`

	// 芯烨云密钥，订单条打印机服务
	XinyeyunUsername string `yaml:"xinyeyunUsername"`
	XinyeyunApiKey   string `yaml:"xinyeyunApiKey"`
}

var Conf = initConfig()

func initConfig() *config {
	// if IsDevEnv() {
	// 	return &config{
	// 		MysqlAddress:              "localhost:3306",
	// 		MysqlDBName:               "shop",
	// 		MysqlUsername:             "root",
	// 		MysqlPassword:             "root",
	// 		RedisAddress:              "localhost:6379",
	// 		RedisPassword:             "",
	// 		TencentCloudAppId:         "",
	// 		TencentCloudSecretId:      "",
	// 		TencentCloudSecretKey:     "",
	// 		TencentCloudCosDomain:     "",
	// 		TencentCloudSmsAppId:      "",
	// 		TencentCloudSmsAppKey:     "",
	// 		TencentCloudSmsTemplateId: "",
	// 		TencentCloudSmsSignName:   "",
	// 		TencentAuth20AppId:        "",
	// 		TencentAuth20AppKey:       "",
	// 	}
	// }
	return getConfigFromGitee("shop-config.yaml", "mh52ypxnuirfo98alzqgv51")
}

// getConfigFromGitee 从gitee上获取配置文件信息，gitee上的代码片段里查看文件名-打开文件后url后缀就是gistId
func getConfigFromGitee(fileName, gistId string) *config {
	var accessToken = os.Getenv("giteeToken") // gitee私人令牌，可以访问 Gitee Open API, 不要泄露出去， see: https://gitee.com/profile/personal_access_tokens
	if accessToken == "" {
		panic("giteeToken为空，无法从gitee上拉取配置")
	}

	url := fmt.Sprintf(`https://gitee.com/api/v5/gists/%s?access_token=%s`, gistId, accessToken) // see: https://gitee.com/api/v5/swagger#/getV5GistsId
	resp := new(struct {
		Files map[string]struct {
			Content string
		}
	})
	_ = json.Unmarshal([]byte(httputil.Get(url)), resp)
	conf := new(config) // 反序列化成config对象
	_ = yaml.Unmarshal([]byte(resp.Files[fileName].Content), conf)
	// seelog.Debugf("成功获取项目配置信息，config:%s", configData)
	if conf.MysqlUsername == "" || conf.MysqlPassword == "" || conf.MysqlDBName == "" {
		panic("配置文件加载为空，请检查网络和gitee上是否存在配置文件")
	}
	return conf
}
